package cn.wtg.kotlinlib.baseUi

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import cn.wtg.kotlinlib.eventBus.Event
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * author: wtg
 * time: 2022/11/22
 * desc:
 */
abstract class BaseBindingActivity<out B : ViewDataBinding> : BaseActivity() {
    protected var binding: @UnsafeVariance B? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, getLayoutRes())
        initView(savedInstanceState)
    }

    protected fun registerEventBus() {
        EventBus.getDefault().register(this)
    }

    /**
     * 消息通知
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMessageEvent(event: Event?) {
    }

    @LayoutRes
    abstract fun getLayoutRes(): Int

    abstract fun initView(savedInstanceState: Bundle?)

    override fun onDestroy() {
        super.onDestroy()
        binding = null
        EventBus.getDefault().unregister(this)
    }
}
